package com.fz.service.impl;

import com.fz.controller.request.BaseRequest;
import com.fz.exception.ServiceException;
import com.fz.mapper.BookMapper;
import com.fz.mapper.BorrowMapper;
import com.fz.mapper.UserMapper;
import com.fz.pojo.Book;
import com.fz.pojo.Borrow;
import com.fz.pojo.User;
import com.fz.service.BorrowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Borrow> list() {
        return borrowMapper.list();
    }

    //分页
    @Override
    public PageInfo<Borrow> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Borrow> borrows = borrowMapper.listByCondition(baseRequest);
        for (Borrow borrow : borrows) {
//            Date returnDate = borrow.getReturnDate();
            LocalDate returnDate = borrow.getReturnDate();
            //当前日期
//            Date now = new Date();
            LocalDate now = LocalDate.now();


//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(now);
//
//            calendar.add(Calendar.DATE, 1);//当前时间往后增加1天
//            Date time = calendar.getTime();//往后增加的结果

//            当前时间加一天等于还书时间 就表示还有一天图书即将到期
            //(returnDate.getTime()-now.getTime())/(24 * 60 * 60 * 1000)==1
            if (now.plusDays(1).isEqual(returnDate)) {
                borrow.setNote("即将到期");
            } else if (now.isEqual(returnDate)) {
                borrow.setNote("已到期");
            } else if (now.isAfter(returnDate)) {
                borrow.setNote("已过期");
            }else {
                borrow.setNote("正常");
            }
        }
        return new PageInfo<>(borrows);
    }

    //添加借书
    @Override
    //添加事务保证数据的安全一致性
    @Transactional
    public void addBorrow(Borrow borrow) {
        //1.校验用户是否存在
        String username = borrow.getUserNo();
        User user = userMapper.getByUsername(username);
        if (Objects.isNull(user)) {
            throw new ServiceException("用户不存在");
        }
        //2.校验图书是否存在
        Book book = bookMapper.getByNo(borrow.getBookNo());
        if (Objects.isNull(book)) {
            throw new ServiceException("所借图书不存在");
        }
        //3.校验图书的数量是否足够
        if (book.getNums() < 1) {
            throw new ServiceException("图书数量不足");
        }
        Integer account = user.getAccount();
        Integer score = book.getScore() * borrow.getDays();//借1本*天数 的总金额
        //4.校验账户余额是否足够
        if (score > account) {
            throw new ServiceException("所需金额不足");
        }

        //更新余额
        user.setAccount(user.getAccount() - score);
        userMapper.updateById(user);
        //更新图书数量
        book.setNums(book.getNums() - 1);
        bookMapper.updateById(book);

        //更新归还时间
//        Calendar calendar = Calendar.getInstance();
//        Date date = new Date();
//        calendar.setTime(date);
//
//        calendar.add(Calendar.DATE, borrow.getDays());//当前时间往后增加
//        Date time = calendar.getTime();//往后增加的结果
//        borrow.setReturnDate(time);
        borrow.setReturnDate(LocalDate.now().plus(borrow.getDays(), ChronoUnit.DAYS));//当前日期加days得到归还日期
        borrow.setScore(score);
        //新增借书记录
        borrowMapper.addBorrow(borrow);


    }

    //通过id查询用户
    @Override
    public Borrow getById(Integer id) {
        return borrowMapper.getById(id);


    }

    @Override
    public void update(Borrow borrow) {
        borrow.setUpdateTime(new Date());
        borrowMapper.updateById(borrow);
    }

    @Override
    public void deleteById(Integer id) {
        borrowMapper.deleteById(id);
    }


}
